package com.practice.bootdemo.controller;

import com.practice.bootdemo.common.R;
import com.practice.bootdemo.dto.CodeTestDto;
import com.practice.bootdemo.dto.CodeTestPageDto;
import com.practice.bootdemo.entity.CodeTest;
import com.practice.bootdemo.service.CodeTestService;
import com.practice.bootdemo.vo.CodeTestVo;
import com.util.BeanExtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* @author :zhoushunquan
* @description: 用户代码生成测试
* @create date: 2022-03-24 17:25:33
*/


@RestController
@RequestMapping(value = "codeTest")
@Tag(description = "CodeTestController", name = "用户代码生成测试")
public class CodeTestController {

    @Autowired
    CodeTestService codeTestService;

    @Operation(summary = "新增测试数据")
    @PostMapping(value = "add")
    public R<Integer> add(@RequestBody @Validated(CodeTestDto.Add.class) CodeTestDto codeTestDto ){
        return codeTestService.add(BeanExtUtil.copyTo(codeTestDto, CodeTest.class)) > 0?
        R.ok(1):R.failed("err");
    }

    @Operation(summary = "修改测试数据")
    @PostMapping(value = "update")
    public R<Integer> update(@RequestBody @Validated(CodeTestDto.Update.class) CodeTestDto codeTestDto){
        return codeTestService.update(BeanExtUtil.copyTo(codeTestDto, CodeTest.class)) > 0?
                R.ok(1):R.failed("err");
     }

    @Operation(summary = "删除测试数据")
    @PostMapping(value = "del/{id}")
    public R <Integer> del(@PathVariable Long id ){
        return codeTestService.delete(id)> 0?
                R.ok(1):R.failed("err");
    }

    @Operation(summary = "查询测试数据详情")
    @GetMapping(value = "detail/{id}")
    public R<CodeTestVo> detail(@PathVariable Long id ){
        return R.ok(codeTestService.detail(id));
    }

    @Operation(summary = "查询测试数据列表")
    @PostMapping(value = "list")
    public R<List<CodeTestVo>> findList(@RequestBody  CodeTestDto codeTestDto ){
        return R.ok(codeTestService.findList(BeanExtUtil.copyTo(codeTestDto, CodeTest.class)));
    }

    @Operation(summary = "查询测试数据分页")
    @PostMapping(value = "pagelist")
    public R findPageList(@RequestBody CodeTestPageDto codeTestPageDto ){
        Integer pageNum = codeTestPageDto.getPageNum();
        Integer pageSize = codeTestPageDto.getPageSize();
        codeTestPageDto.setPageNum(pageNum==null?0:pageNum);
        codeTestPageDto.setPageSize(pageSize==null?10:pageSize);
        return R.ok( codeTestService.findPageList(codeTestPageDto));
    }
}